package cn.com.soole.demo;

import cn.com.soole.demo.client.StuMqttClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class MqttDemoApplication implements EnvironmentAware {

    static Environment environment;

    public static void main( String[] args ) {
        SpringApplication.run(MqttDemoApplication.class);
        String connNum = environment.getProperty("conn.num") == null ? "1" : environment.getProperty("conn.num");
        String hostIp = environment.getProperty("host.ip") == null ? "222" : environment.getProperty("host.ip");
        if (null == hostIp || hostIp.isEmpty()) {
            System.out.println("ip 不能为空");
            return ;
        }
        System.out.println("将创建: " + connNum + "连接");
        if (null != connNum && !connNum.isEmpty()) {
            int threadCount = Integer.parseInt(connNum);
            ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
            for (int i = 0; i < threadCount; i++) {
                executorService.execute(() -> {
                    StuMqttClient stuMqttClient = new StuMqttClient(hostIp);
                });
            }
            executorService.shutdown();
        }
    }

    @Override
    public void setEnvironment(Environment environment) {
        MqttDemoApplication.environment = environment;
    }
}
